package tnb.android;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.util.Log;

public class ClientConnectionUtil 
{
	//Connection variables
	private static HttpClient httpClient = new DefaultHttpClient();
	private static HttpGet httpGet;
	private static HttpResponse httpResponse;
	
	//Create a new account
	public static void createAccount(String requestedName, String requestedPassword)
	{
		httpGet = new HttpGet();
		
		//Add headers containing all data which should be sent
		httpGet.addHeader(GTK.NetworkProtocol.REQUEST_HEADER, GTK.NetworkProtocol.CREATE_ACCOUNT_REQUEST);
		httpGet.addHeader(GTK.NetworkProtocol.NAME_HEADER, requestedName);
		httpGet.addHeader(GTK.NetworkProtocol.PASSWORD_HEADER, requestedPassword);
		
		//Send data and read response
		try 
		{
			httpGet.setURI(new URI(GTK.NetworkProtocol.SERVER_URL));
            httpResponse = httpClient.execute(httpGet);
            
			BufferedReader in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
		    String response = in.readLine();
		    
		    //Log.i("Authentication", "Response:" + response);
		    
		    if(response.equals(GTK.NetworkProtocol.SUCCESS))
		    {
		    	Log.i("Account creation", "Account was created successfully");
		    }
		    else
		    {
		    	Log.i("Account creation", "Account was not created");
		    }
		    
		    in.close();
		} 
        catch (ClientProtocolException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); } 
        catch (URISyntaxException e) {e.printStackTrace(); }
	}
	public static void earnAchievement(byte achievementID, String userName, String password)
	{
		httpGet = new HttpGet();
		
		//Add headers containing all data which should be sent
		httpGet.addHeader(GTK.NetworkProtocol.REQUEST_HEADER, GTK.NetworkProtocol.EARN_ACHIEVEMENT_REQUEST);
		httpGet.addHeader(GTK.NetworkProtocol.NAME_HEADER, userName);
		httpGet.addHeader(GTK.NetworkProtocol.PASSWORD_HEADER, password);
		httpGet.addHeader(GTK.NetworkProtocol.ACHIEVEMENT_ID_HEADER, "" + achievementID);
		
		//Send data and read response
		try 
		{
			httpGet.setURI(new URI(GTK.NetworkProtocol.SERVER_URL));
            httpResponse = httpClient.execute(httpGet);
            
			BufferedReader in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
		    String response = in.readLine();
		    
		    //Log.i("Achievement earning", "Response: " + response);
		    if(response.equals(GTK.NetworkProtocol.SUCCESS))
		    {
		    	Log.i("Achievement earned!", "Achievement: " + achievementID); //achievementID should go into some string map or something to display name osv
		    }
		    else
		    {
		    	Log.i("Achievement was not earned", "Achievement earning failed"); //Could log more, server gives GTK response of specific error	
		    }
		    
		    in.close();
		}
        catch (ClientProtocolException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); } 
        catch (URISyntaxException e) {e.printStackTrace(); }
	}
	
	public static List<Byte> getAchievments(String userName, String password)
	{
		List<Byte> achievements = new ArrayList<Byte>();
		
		httpGet = new HttpGet();
		
		//Add headers containing all data which should be sent
		httpGet.addHeader(GTK.NetworkProtocol.REQUEST_HEADER, GTK.NetworkProtocol.GET_ACHIEVEMENTS_REQUEST);
		httpGet.addHeader(GTK.NetworkProtocol.NAME_HEADER, userName);
		httpGet.addHeader(GTK.NetworkProtocol.PASSWORD_HEADER, password);
		
		//Send data and read response
		String response = "";
		try 
		{
			httpGet.setURI(new URI(GTK.NetworkProtocol.SERVER_URL));
            httpResponse = httpClient.execute(httpGet);
            
			BufferedReader in = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent()));
		    response = in.readLine();
		    
		    //Log.i("Achievement earning", "Response: " + response);
		    if(response.equals(GTK.NetworkProtocol.SUCCESS))
		    {
		    	String serverResponse = in.readLine();
		    	
		    	Log.i("Received achievement data", serverResponse);
		    	
		    	String[] achievementData = serverResponse.split("|");
		    	
		    	for (int i = 0; i < achievementData.length; i++)
		    	{
		    		try
		    		{
		    			achievements.add(Byte.parseByte(achievementData[i]));
		    		}
		    		catch (Exception e){}
		    	}
		    }
		    else
		    {
		    	Log.i("Failed to get achievements", "Failed to get achievements"); //Could log more, server gives GTK response of specific error
		    }
		    in.close();
		}
        catch (ClientProtocolException e) { e.printStackTrace(); } 
        catch (IOException e) { e.printStackTrace(); } 
        catch (URISyntaxException e) {e.printStackTrace(); }

		return achievements;
	}
}
